package com.example.user.myhjjf_app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.district.DistrictResult;
import com.baidu.mapapi.utils.SpatialRelationUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Area;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.model.Model_Tagging;
import com.example.user.myhjjf_app.presenter.Presenter_District;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Activity_District extends AppCompatActivity {
    Model_Area model_area;
    Model_Tagging tagging;
    @BindView(R.id.bmapView)
    MapView bmapView;
    public static List<String> id;
    public static List<List<LatLng>> lists;
    String dd;
    public static BaiduMap baiduMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__district);
        ButterKnife.bind(this);
        baiduMap = bmapView.getMap();
        baiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                for (int i = 0; i <lists.size(); i++) {
                    List<LatLng> latLngs = lists.get(i);
                    if (SpatialRelationUtil.isPolygonContainsPoint(latLngs, latLng)) {
                        Model_Regional.getRegional().setZoneId(id.get(i));
                       ActivityUtils.startActivity(Activity_Tagging.class);
                        return;
                    }
                }
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        DistrictResult districtResult = getIntent().getParcelableExtra("districtResult");
        show(districtResult);
    }

    public void show(DistrictResult districtResult) {
        String id = getId(districtResult.getCityName());
        Model_Regional.getRegional().setDepartId(id);
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/map/forward.do"))
                .params("id", id)
                .params("level", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        model_area = JSON.parseObject(response.body(), Model_Area.class);
                        Presenter_District district = new Presenter_District(model_area);
                        district.setMap();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Toast.makeText(Activity_District.this, "获取失败", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public String getId(String CityName) {
        String id = null;
        switch (CityName) {
            case "册亨县":
                id = "4028838656b6a33e0156b6d0cad10012";
                break;
            case "望谟县":
                id = "4028838656b6a33e0156b6d094ea0010";
                break;
            case "贞丰县":
                id = "4028838656b6a33e0156b6d11b900016";
                break;
            case "安龙县":
                id = "4028838656b6a33e0156b6cb7f37000c";
                break;
            case "兴义市":
                id = "8a8ab0b246dc81120146dc8180a20016";
                break;
            case "兴仁县":
                id = "4028838656b6a33e0156b6d0fa890014";
                break;
            case "晴隆县":
                id = "4028838656b6a33e0156b6d16769001a";
                break;
            case "普安县":
                id = "4028838656b6a33e0156b6d145890018";
                break;
        }

        return id;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        bmapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        bmapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bmapView.onPause();
    }
}
